package com.controller.list;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.splwg.base.api.datatypes.DateTime;
import com.splwg.base.api.service.DataElement;
import com.splwg.base.api.service.ItemList;
import com.splwg.base.api.service.PageHeader;
import com.splwg.cmun.common.JdbcCrudTools;
import com.splwg.shared.common.ApplicationError;
import com.splwg.shared.common.LoggedException;
import com.splwg.shared.logging.Logger;
import com.splwg.shared.logging.LoggerFactory;


/**
*
@QueryPage (program = ${tableName2}_LIST, service = ${tableName2}_LIST, modules = {},
    header = {@DataField (name = UN_CII_002_MAIN_ID),@DataField (name = UN_CII_002_TASK_NAME),@DataField (name = UN_CII_002_INFO),@DataField (name = UN_CII_002_STATUS),@DataField (name = UN_CII_002_COUTENT)},
    headerFields = {@DataField (name = UN_CII_002_MAIN_ID),@DataField (name = UN_CII_002_TASK_NAME),@DataField (name = UN_CII_002_INFO),@DataField (name = UN_CII_002_STATUS),@DataField (name = UN_CII_002_COUTENT)},
    lists = {@List (name = allList, size = 10,
        body = @DataElement (contents = {@DataField (name = UN_CII_002_MAIN_ID)
                    , @DataField (name = UN_CII_002_TASK_NAME)
                    , @DataField (name = UN_CII_002_INFO)
                    , @DataField (name = UN_CII_002_STATUS)
                    , @DataField (name = UN_CII_002_COUTENT)
                    , @DataField (name = UN_CII_002_START_TIME)
                    , @DataField (name = UN_CII_002_END_TIME)
            })
    )},
    body = @DataElement (contents = {@ListField (name = allList)}),
    actions = { "read" })
*/
public class UnCiiT002TaskListController extends UnCiiT002TaskListController_Gen {
    private static final Logger logger = LoggerFactory.getLogger(UnCiiT002TaskListController.class);
    private String tableName = "UN_CII_T_002_TASK";

    @Override
    protected DataElement read(PageHeader header) throws ApplicationError {
            DataElement element = new DataElement();
            Connection connection = JdbcCrudTools.getNewConnection();
            PreparedStatement statement = null;
            logger.info("读取进入了---------------------------");
            logger.info("come in " + this.getClass().getName() + " read Method");
                            
      String UN_CII_002_MAIN_ID = header.getString(STRUCTURE.HEADER.UN_CII_002_MAIN_ID);
                                            
      String UN_CII_002_TASK_NAME = header.getString(STRUCTURE.HEADER.UN_CII_002_TASK_NAME);
                                            
      String UN_CII_002_INFO = header.getString(STRUCTURE.HEADER.UN_CII_002_INFO);
                                            
      String UN_CII_002_STATUS = header.getString(STRUCTURE.HEADER.UN_CII_002_STATUS);
                                            
      String UN_CII_002_COUTENT = header.getString(STRUCTURE.HEADER.UN_CII_002_COUTENT);
                                                                                    
            StringBuilder sql = new StringBuilder("SELECT * from " + tableName);
            logger.info("Sql:"+sql);
            List<DataElement> rawList = new ArrayList<DataElement>();
            DataElement root = new DataElement();
            try {
                sql.append(" WHERE 1=1");
                
                if(StringUtils.isNotEmpty(UN_CII_002_MAIN_ID)){
                    sql.append(" and UN_CII_002_MAIN_ID like ? ");
                }
                    
                if(StringUtils.isNotEmpty(UN_CII_002_TASK_NAME)){
                    sql.append(" and UN_CII_002_TASK_NAME like ? ");
                }
                    
                if(StringUtils.isNotEmpty(UN_CII_002_INFO)){
                    sql.append(" and UN_CII_002_INFO like ? ");
                }
                    
                if(StringUtils.isNotEmpty(UN_CII_002_STATUS)){
                    sql.append(" and UN_CII_002_STATUS like ? ");
                }
                    
                if(StringUtils.isNotEmpty(UN_CII_002_COUTENT)){
                    sql.append(" and UN_CII_002_COUTENT like ? ");
                }
                                    
                statement = connection.prepareStatement(sql.toString());
                int index = 1;
                
                if(StringUtils.isNotEmpty(UN_CII_002_MAIN_ID)){
                    statement.setString(index++, "%"+UN_CII_002_MAIN_ID+"%");
                }
                    
                if(StringUtils.isNotEmpty(UN_CII_002_TASK_NAME)){
                    statement.setString(index++, "%"+UN_CII_002_TASK_NAME+"%");
                }
                    
                if(StringUtils.isNotEmpty(UN_CII_002_INFO)){
                    statement.setString(index++, "%"+UN_CII_002_INFO+"%");
                }
                    
                if(StringUtils.isNotEmpty(UN_CII_002_STATUS)){
                    statement.setString(index++, "%"+UN_CII_002_STATUS+"%");
                }
                    
                if(StringUtils.isNotEmpty(UN_CII_002_COUTENT)){
                    statement.setString(index++, "%"+UN_CII_002_COUTENT+"%");
                }
                                    
                logger.info("Sql:"+sql.toString());
                ResultSet result = statement.executeQuery();
                while (result.next()){
                    element = new DataElement();
                    
                    element.put(STRUCTURE.list_allList.UN_CII_002_MAIN_ID , result.getString("UN_CII_002_MAIN_ID"));
                    
                    element.put(STRUCTURE.list_allList.UN_CII_002_TASK_NAME , result.getString("UN_CII_002_TASK_NAME"));
                    
                    element.put(STRUCTURE.list_allList.UN_CII_002_INFO , result.getString("UN_CII_002_INFO"));
                    
                    element.put(STRUCTURE.list_allList.UN_CII_002_STATUS , result.getString("UN_CII_002_STATUS"));
                    
                    element.put(STRUCTURE.list_allList.UN_CII_002_COUTENT , result.getString("UN_CII_002_COUTENT"));
                    
                    element.put(STRUCTURE.list_allList.UN_CII_002_START_TIME , result.getString("UN_CII_002_START_TIME"));
                    
                    element.put(STRUCTURE.list_allList.UN_CII_002_END_TIME , result.getString("UN_CII_002_END_TIME"));
                                        //element.put(STRUCTURE.list_allList.时间, DateTime.fromResultSet(result, "时间"));
                    rawList.add(element);
                }
                ItemList<DataElement> itemList = root.newList("allList");
                root.addList(itemList);
                itemList.setList(rawList);
                logger.info(root);
            } catch (Exception e) {
                throw LoggedException.wrap(logger, "Failed execution of the SQL prepared statement " + sql
                + " bound to values " + tableName + " Unexpected SQLException", e);
            } finally {
                JdbcCrudTools.close(connection,statement);
            }
            logger.info("读取结束了---------------------------");
            return root;
     }

}